package com.frame.service.third.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.frame.common.RespEnum;
import com.frame.common.constant.ThirdPartyConstants;
import com.frame.common.enums.ThirdPartyType;
import com.frame.common.exception.ServerException;
import com.frame.http.HttpUtil;
import com.frame.model.third.vo.ThirdPartyUserVO;
import com.frame.service.third.ThirdPartyService;

/**
 * 第三方接口
 * @author Flying
 *
 */
public class ThirdPartyServiceImpl implements ThirdPartyService{

	/**
     * 获取QQ用户信息
     *
     * @param token
     * @param openid
     */
	@Override
    public  ThirdPartyUserVO getQQUserinfo(String token, String openid,String qqAppId)  {
		ThirdPartyUserVO user = new ThirdPartyUserVO();
        String url = ThirdPartyConstants.QQ_URL;
        url = url + "?format=json&access_token=" + token + "&oauth_consumer_key="
                + qqAppId + "&openid=" + openid;
        String res = HttpUtil.get(url);
        JSONObject json = JSON.parseObject(res);
        if (json.getIntValue("ret") == 0) {
            user.setUserName(json.getString("nickname"));
            String img = json.getString("figureurl_qq_2");
            if (img == null || "".equals(img)) {
                img = json.getString("figureurl_qq_1");
            }
            user.setAvatarUrl(img);
            String sex = json.getString("gender");
            if ("女".equals(sex)) {
                user.setGender(0);
            } else {
                user.setGender(1);
            }
        } else {
            throw new ServerException(RespEnum.USER_QQ_THIRD_INFO_ERROR);
        }
        user.setThirdPartyType(ThirdPartyType.QQ);
        user.setRemark(res);
        return user;
    }

    /**
     * 获取微信用户信息 
     * @param token
     * @param openid
     * @return
     */
	@Override
    public  ThirdPartyUserVO getWxUserinfo(String token, String openid) {
        ThirdPartyUserVO user = new ThirdPartyUserVO();
        String url = ThirdPartyConstants.WINXIN_URL;
        url = url + "?access_token=" + token + "&openid=" + openid;
        String res = HttpUtil.get(url);
        JSONObject json = JSON.parseObject(res);
        if (json.getString("errcode") == null) {
            user.setUserName(json.getString("nickname"));
            String img = json.getString("headimgurl");
            if (img != null && !"".equals(img)) {
                user.setAvatarUrl(img);
            }
            String sex = json.getString("sex");
            if ("0".equals(sex)) {
                user.setGender(0);
            } else {
                user.setGender(1);
            }
            user.setOpenid(json.getString("unionid"));
        } else {
            throw new ServerException(RespEnum.USER_WE_CHART_THIRD_INFO_ERROR);
        }
        user.setRemark(res);
        user.setThirdPartyType(ThirdPartyType.WX);
        return user;
    }

    /**
     * 获取新浪用户信息
     *
     * @param token
     * @param uid
     * @return
     */
	@Override
    public  ThirdPartyUserVO getSinaUserinfo(String token, String uid)  {
        ThirdPartyUserVO user = new ThirdPartyUserVO();
        String url = ThirdPartyConstants.SINA_URL;
        url = url + "?access_token=" + token + "&uid=" + uid;
        String res = HttpUtil.get(url);
        JSONObject json = JSON.parseObject(res);
        String name = json.getString("name");
        String nickName = StringUtils.isBlank(json.getString("screen_name")) ? name : json.getString("screen_name");
        user.setAvatarUrl(json.getString("avatar_large"));
        user.setUserName(nickName);
        if ("f".equals(json.getString("gender"))) {
            user.setGender(0);
        } else {
            user.setGender(1);
        }
        user.setToken(token);
        user.setOpenid(uid);
        user.setThirdPartyType(ThirdPartyType.SINA);
        user.setRemark(res);
        return user;
    }



    /**
     * 将格式为s1&s2&s3...的字符串转化成Map集合
     *
     * @param str
     * @return
     */
    private  Map<String, String> toMap(String str) {
        Map<String, String> map = new HashMap<String, String>();
        String[] strs = str.split("&");
        for (String str2 : strs) {
            String[] ss = str2.split("=");
            map.put(ss[0], ss[1]);
        }
        return map;
    }
}
